/*
package com.example.auth.decryptparam;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.HashMap;
import java.util.Map;
*/
/**
 * 加密解密参数值
 *//*

public class ConfigParamDecrypt implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    private Logger logger = LoggerFactory.getLogger(ConfigParamDecrypt.class);
    */
/**
     * 加密算法AES
     *//*

    private static final String KEY_ALGORITHM = "AES";
    private String secretKey;
    */
/**
     * key的长度，Wrong key size: must be equal to 128, 192 or 256
     * 传入时需要16、24、36
     *//*

    private static final Integer KEY_LENGTH = 16 * 8;

    */
/**
     * 算法名称/加密模式/数据填充方式
     * 默认：AES/ECB/PKCS5Padding
     *//*

    private static final String ALGORITHMS = "AES/ECB/PKCS5Padding";
    public ConfigParamDecrypt(){

    }
    */
/**
     * 不能在代码中创建
     * JceSecurity.getVerificationResult 会将其put进 private static final Map<Provider,Object>中，导致内存缓便被耗尽
     *//*

    private static final BouncyCastleProvider PROVIDER = new BouncyCastleProvider();

  // 从配置文件中读取密钥


    */
/**
     * 解密
     *
     * @param encryptStr 解密的字符串
     * @param decryptKey 解密的key值
     *//*

    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
        //base64格式的key字符串转byte
        byte[] decodeBase64 = Base64.decodeBase64(encryptStr);

        //设置Cipher对象
        Cipher cipher = Cipher.getInstance(ALGORITHMS,PROVIDER);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), KEY_ALGORITHM));
        //调用doFinal解密
        return new String(cipher.doFinal(decodeBase64));
    }
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
        ConfigurableEnvironment  environment  =configurableApplicationContext.getEnvironment();
        MutablePropertySources propertySources = environment.getPropertySources();
        Map<String, Object> newProperties = new HashMap<>();
        for (PropertySource<?> source : propertySources) {
            if (source instanceof MapPropertySource) {
                Map<String, Object> map = ((MapPropertySource) source).getSource();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    if (key.equals("aes_secret_key")){
                        secretKey = String.valueOf(value);
                    }
                    // 检查key是否包含加密标记，并且value是字符串
                    if (key.contains("password") &&String.valueOf(value).contains("ENC(") && String.valueOf(value).endsWith(")")) {
                        String encryptedValue = String.valueOf(value);
                        String decryptedValue = null;
                        try {
                            decryptedValue = decrypt(encryptedValue.substring(4, encryptedValue.length() - 1),secretKey);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        // 注意：这里我们不能直接修改map，因为迭代时修改会导致ConcurrentModificationException
                        // 所以我们创建一个新的MapPropertySource并替换旧的
                        newProperties.put(key,decryptedValue);
                        //解密后的属性
                        PropertySource<?> newPropertySource = new MapPropertySource("customSource", newProperties);
                        propertySources.addFirst(newPropertySource); // 或者使用 addBefore/addAfter 来指定位置
                    }
                }
            }
        }

    }
}
*/
